package com.bugs.processor;

import com.bugs.reptile.Parser;
import com.model.JobInfo;
import com.model.WorkBug;
import lombok.extern.java.Log;
import org.assertj.core.util.Strings;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

/**
 * 前程无忧
 */
@Log
public class Job51Processor extends BaseProcessor implements PageProcessor {

    public Job51Processor(Parser reptile, WorkBug workBug) {
        super(reptile, workBug);
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setSleepTime(1000);
    }

    private void addPagination(Page page, String url) {
        if(Strings.isNullOrEmpty(url)) return;
        pagesType.put(url, PageType.Pagination);
        page.addTargetRequest(url);
    }

    private void addDetailPage(Page page, String url) {
        if(Strings.isNullOrEmpty(url)) return;
        pagesType.put(url, PageType.DetailPage);
        page.addTargetRequest(url);
    }

    @Override
    public void process(Page page) {
        PageType pageType = this.pagesType.remove(page.getRequest().getUrl());
        Html html = page.getHtml();
        if(pageType == PageType.Pagination) {
            // 解析每一项url
            List<String> infoUrls = reptile.getDetailUrls(html);
            if(infoUrls != null) {
                infoUrls.forEach((url) -> addDetailPage(page, url));
            }
            // 添加下一页url
            if(reptile.hasNextPage(html)) {
                addPagination(page, reptile.nextPageUrl(html, null));
            }
        }else if(pageType == PageType.DetailPage) {
            // 解析jobInfo
            JobInfo jobInfo = reptile.convertJobInfo(html);
            jobInfo.setCaptureUrl(page.getRequest().getUrl());
            jobInfo.setWorkBugId(workBug.getId());
            jobInfo.setReptileClass(workBug.getReptileClass());
            jobInfo.createHashcode();

            super.setLngLatAndSave(jobInfo);
        }
    }
}
